package com.dhecp.project.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.core.utils.qywx.QyWxApiUrls;
import com.dhecp.core.utils.qywx.QyWxHttpUtils;
import com.dhecp.core.utils.qywx.WeChatUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;

/**
* 企业微信服务端api
* @author 0431
*/
@Controller
@RequestMapping("/system/wechat")
public class WeChatController extends BaseModuleController{

	/**  获取所有部门
	* @return AjaxResult
	*/
	@RequestMapping("/deptlist")
	@ResponseBody
	public AjaxResult queryDeptList() {
		String token = WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.TXLSECERT,WeChatUtils.TXLCACHEKEY);
		String requestUrl = QyWxApiUrls.DEPT_GET_URL.replace("ACCESS_TOKEN", token);
		JSONObject jsonObject = QyWxHttpUtils.sendGet(requestUrl);
		if(jsonObject.get("errcode").toString().equals("0")) {
			return AjaxResult.success(jsonObject.get("department"));
		}else return AjaxResult.success(jsonObject);
	}
	
	/**  获取所有成员
	* @return AjaxResult
	*/
	@RequestMapping("/userlist")
	@ResponseBody
	public AjaxResult queryUserList() {
		String token = WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.TXLSECERT,WeChatUtils.TXLCACHEKEY);
		String requestUrl = QyWxApiUrls.DEPT_GET_URL.replace("ACCESS_TOKEN", token);
		JSONObject jsonObject = QyWxHttpUtils.sendGet(requestUrl);
		if(jsonObject.get("errcode").toString().equals("0")) {
			List<Object> userList = new ArrayList<> ();
			for(Object jo : JSONArray.parseArray(jsonObject.get("department").toString(), Object.class)) {
				if(JSONObject.parseObject(JSONObject.toJSONString(jo), Map.class).get("parentid").toString().equals("0")) {//父级ID为0则为一级部门或公司
					requestUrl = QyWxApiUrls.USER_GET_URL.replace("ACCESS_TOKEN", token).replace("DEPARTMENT_ID", JSONObject.parseObject(JSONObject.toJSONString(jo), Map.class).get("id").toString()).replace("FETCH_CHILD", "1");
					jsonObject = QyWxHttpUtils.sendGet(requestUrl);
					userList.addAll(JSONArray.parseArray(jsonObject.get("userlist").toString(), Object.class));
				}
			}
			return AjaxResult.success(userList);
		}else return AjaxResult.success(jsonObject);
	}
	
	/**  发送应用消息
	* @return AjaxResult
	*/
	@RequestMapping("/send")
	@ResponseBody
	public AjaxResult send(@RequestParam Map<String, Object> params) {
		String token = WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.H5SECERT, WeChatUtils.H5CACHEKEY);
		//发起请求
		String requestUrl = QyWxApiUrls.MESSAGE_SEND_URL.replace("ACCESS_TOKEN", token);
		String[] keyArr = {"touser","toparty","totag","agentid","safe","enable_id_trans","enable_duplicate_check","duplicate_check_interval","msgtype"};
		for(String key : params.keySet()) {
			if(!Arrays.asList(keyArr).contains(key)) {
				params.put(key, JSONArray.parse(params.get(key).toString()));
			}
		}
		String postStr = JSONArray.toJSONString(params);
		JSONObject jsonObject = QyWxHttpUtils.sendPost(requestUrl, postStr);
		return AjaxResult.success(jsonObject);
	}
}